﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;

/// <summary>
/// Summary description for EventAPI
/// </summary>
public class EventAPI//fixme: put all this file in the right place
{
    public const string CONNECTION_STRING = @"https://www.eventbrite.com/xml/";
    /// <summary>
    /// The secure method of authentication hides your login information and instead passes a user_key into the api.
    /// If a user_key provided, the API will validate it against the Eventbrite user database.
    /// If the user_key is valid and authenticated, the method call will continue. 
    /// If the user_key is not valid or presented an incorrect password and the method requires authentication, an XML error message is returned:
    /// To ensure maximum safety, only authentication requests made over SSL will be processed. 
    /// So all your API calls should start with "https://".
    /// </summary>
    public const string SECURE_AUTHENTICATION_URL = @"?app_key={0}&user_key={1}";
    #region Authentication Keys

    /// <summary>
    /// The application key is a string assigned by Eventbrite which identifies the application making the method call.
    /// This is separate from user authentication, which identifies the Eventbrite user using the application. For example, if Eventbrite user xyz@mail.com is using a Dashboard widget to display events from a Smart Calendar, "app_key" would identify the Dashboard widget and "user"/"password" would identify the user.
    /// A valid application key is required for any interaction with the Eventbrite API
    /// </summary>
    public const string APP_KEY = @"ZTczMmJlM2EzYWYz";

    /// <summary>
    /// The user key is a unique string assigned by Eventbrite which identifies the user making an API call.
    /// This replaces basic username/password authentication.
    /// A valid user key is the recommended method for user authentication with the Eventbrite API.
    /// </summary>
    public const string USER_KEY = @"12662331703440714269";
    #endregion

    public EventAPI()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    #region Event Methods
    public const string NEW_EVENT_URL = @"{0}event_new{1}&title={2}&description={3}&start_date={4}&end_date={5}&timezone={6}&privacy={7}&status={8}";
    public string EventNew(string EventTitle, string EventDescription, string StartDate, string EndDate, string TimeZone, int Privacy)
    {
        return GetWebServiceResponseAsString(string.Format(NEW_EVENT_URL, CONNECTION_STRING, this.SecureClientCredentials(), EventTitle, EventDescription, StartDate, EndDate, TimeZone, Privacy, "live"), 60000);
    }
    #endregion
    #region Service Communication Methodes
    /// <summary>
    /// The simplest way to view the returned data is to get the response stream and put it into a string. This is especially handy for debugging. The following code gets a web page and returns the contents as a string. 
    /// </summary>
    /// <param name="url">The Exact request url</param>
    /// <param name="timeOut">The duration waiting for response from the service</param>
    /// <returns></returns>
    private static string GetWebServiceResponseAsString(string url, int timeOut)
    {
        string ResultText = "";

        // Create the web request
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
        request.Method = "GET";
        request.Timeout = timeOut;

        // Get response 
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader StreamReader = new StreamReader(response.GetResponseStream());
            ResultText = StreamReader.ReadToEnd();
        }

        return ResultText;
    }
    /// <summary>
    /// The secure method of authentication hides your login information and instead passes a user_key into the api.
    /// If a user_key provided, the API will validate it against the Eventbrite user database.
    /// If the user_key is valid and authenticated, the method call will continue. 
    /// If the user_key is not valid or presented an incorrect password and the method requires authentication, an XML error message is returned:
    /// To ensure maximum safety, only authentication requests made over SSL will be processed. 
    /// So all your API calls should start with "https://".
    /// </summary>
    /// <param name="UserKey">The user key is a unique string assigned by Eventbrite which identifies the user making an API call.
    /// This replaces basic username/password authentication.
    /// A valid user key is the recommended method for user authentication with the Eventbrite API.
    ///</param>
    /// <returns>the right SECURE AUTHENTICATION URL for the user</returns>
    public string SecureClientCredentials(string UserKey)
    {
        return string.Format(SECURE_AUTHENTICATION_URL, APP_KEY, UserKey);
    }
    public string SecureClientCredentials()
    {
        return string.Format(SECURE_AUTHENTICATION_URL, APP_KEY, USER_KEY);
    }

    #endregion


    public const string EVENT_LIST_ATTENDEES_URL = @"{0}event_list_attendees{1}&id={2}&count={3}";
    public string EventListAttendees(string EventId)
    {
        return GetWebServiceResponseAsString(string.Format(EVENT_LIST_ATTENDEES_URL , CONNECTION_STRING, this.SecureClientCredentials(), EventId, 50), 60000);
    }
}